package com.dididoctor.doctor.WYY.Timely;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.Activity.Main.ConsultPresenter;
import com.dididoctor.doctor.Activity.Order.DiagnosisSuggest.DiagnosisSuggestActivity;
import com.dididoctor.doctor.Bean.Token;
import com.dididoctor.doctor.MV.BusinessClient;
import com.dididoctor.doctor.MV.FastHttpResponseHandler;
import com.dididoctor.doctor.MV.Response;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Utils.MyToast;
import com.dididoctor.doctor.Utils.SharedPreferencesUtils;
import com.loopj.android.http.RequestParams;
import com.netease.nim.uikit.session.viewholder.MsgViewHolderBase;
import com.netease.nimlib.sdk.msg.constant.MsgStatusEnum;

import org.apache.http.Header;

/**
 * Created by zhoujianghua on 2015/8/4.
 */
public class MsgViewHolderTimely extends MsgViewHolderBase implements View.OnClickListener {

    private TextView tv_content;
    private Button btn_confirm, btn_confirm_drawdiagn;
    private ConsultPresenter presenter;
    private String PatientPhone;
    private String disId;
    private String recId;
    private String patientName;
    private String age;
    private String illName;
    private String sex;
    private String patientHeadPic;

    @Override
    protected int getContentResId() {
        return R.layout.item_timely;

    }

    @Override
    protected void inflateContentView() {

        tv_content = (TextView) view.findViewById(R.id.tv_content);

        btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(this);
        btn_confirm_drawdiagn = (Button) view.findViewById(R.id.btn_confirm_drawdiagn);
        btn_confirm_drawdiagn.setOnClickListener(this);
    }

    @Override
    protected void setLayoutParams(int width, int height, View... views) {
        View ll_medical = view.findViewById(R.id.ll_medical);
//        setLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,ll_medical);
        super.setLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, ll_medical);
    }

    @Override
    protected void bindContentView() {
        TimelyAttachment attachment = (TimelyAttachment) message.getAttachment();
        PatientPhone = attachment.getPatientPhone();
        disId = attachment.getDisId();
        recId = attachment.getRecId();
        patientName = attachment.getPatientName();
        age = attachment.getAge();
        illName = attachment.getIllName();
        patientHeadPic = attachment.getPatientHeadPic();
        sex = attachment.getSex();
        SharedPreferencesUtils.setParam(Token.getMcontext(), "GuessdsiID", disId);
        SharedPreferencesUtils.setParam(Token.getMcontext(), "GuessRecId", recId);
        SharedPreferencesUtils.setParam(Token.getMcontext(), "GuesspatientName", patientName);
        SharedPreferencesUtils.setParam(Token.getMcontext(), "Guessage", age);
        SharedPreferencesUtils.setParam(Token.getMcontext(), "GuessillName", illName);
        SharedPreferencesUtils.setParam(Token.getMcontext(), "GuesspatientHeadPic", patientHeadPic);
        recId = attachment.getRecId();
        tv_content.setText("患者" + attachment.getPatientName() + "与你进行了电话咨询");

    }

    @Override
    protected boolean isMiddleItem() {
        return true;
    }

    @Override
    protected boolean isShowBubble() {
        return false;
    }

    @Override
    protected boolean onItemLongClick() {

        return false;
    }

    @Override
    protected int leftBackground() {
        return 0;
    }

    @Override
    protected int rightBackground() {
        return 0;
    }

    private void layoutByDirection() {
        View ll_medical = findViewById(R.id.ll_medical);
        View tv_guess_medical_click = findViewById(R.id.tv_guess_medical_click);

        ViewGroup container = (ViewGroup) ll_medical.getParent();
        container.removeView(tv_guess_medical_click);
        if (isReceivedMessage()) {
            container.addView(tv_guess_medical_click, 1);
        } else {
            container.addView(tv_guess_medical_click, 0);
        }
        if (message.getStatus() == MsgStatusEnum.success) {
        } else {
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                if (message.getStatus() == MsgStatusEnum.success) {
                    getCalling(PatientPhone);
                }
                break;
            case R.id.btn_confirm_drawdiagn:
                String accId = message.getFromAccount();

                Intent intetn = new Intent(Token.getMcontext(), DiagnosisSuggestActivity.class);
                intetn.putExtra("accId", accId);
                intetn.putExtra("recId", recId);
                intetn.putExtra("disId", disId);
                intetn.putExtra("patientName", patientName);
                intetn.putExtra("age", age);
                intetn.putExtra("illName", illName);
                intetn.putExtra("patientHeadPic", patientHeadPic);
                intetn.putExtra("sex", sex);
                context.startActivity(intetn);
                break;
        }

    }


    public void getCalling(String callee) {
//        userId	String	是	用户id
//        token	String	是	唯一标示
//        reqType	String	是	1-医生拨打患者电话 2-患者拨打医生电话
//        accId	String	是	云信id
//        caller	String	是	主叫电话号码
//        callee	String	是	被叫电话号码
//        hosId	String	否	医生医院id reqType=2时用
//        doctorId	String	否	医生id reqType=2时用
//        recId	String	否	病历id reqType=1时用
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("token", Token.getToken());
        params.put("reqType", "1");
        params.put("accId", Token.getAccId());
        params.put("caller", Token.getPhone());
        params.put("callee", callee);
        params.put("recId", recId);
        BusinessClient.post(ConstantValue.callingUrl, params, new FastHttpResponseHandler() {
            private String hasDoctor = "";//是否有签约医生1有0没有

            @Override
            public void onResponse(Response response) {
                String status = response.getStatus();
                if ("1".equals(status)) {
                    MyToast.DefaultmakeText(context, "正在拨打，请稍后", 0);

                } else {
                    MyToast.DefaultmakeText(context, response.getMsg(), 0);
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                MyToast.DefaultmakeText(context, "拨打失败", 0);
            }
        });
    }
}
